﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;

public class AssetXmlData
{
    public int mainversion;
    public List<Asset> assetDatas = new List<Asset>();
}
//<root>
//    <mainversion>1</mainversion>
//    <Assets>
//        <Asset url="">
// 			  <name>123123</name>
//            <md5>123123123123</md5>
//            <version>1</version>     
//            <size>123123</size>
//            <forse_temp>1</forse_temp>
//			  <dynamic_asset_type>0</dynamic_asset_type>
//			  <asset_type>0</asset_type>
//        </Asset>
//    </Assets>
//</root>


public class AssetXmlOpt
{
    public const string rootTxt = "Root";
    public const string mainVersionTxt = "MainVersion";
    public const string assetsTxt = "Assets";
    public const string subAssetTxt = "Asset";
    public const string md5Txt = "MD5";
    public const string urlTxt = "Url";
    public const string versionTxt = "Version";
    public const string nameTxt = "Name";
    public const string sizeTxt = "Size";
    public const string forceTempTxt = "force_temp";
    public const string dynamicassettypeTxt = "dynamic_asset_Type";
    public const string assettypeTxt = "asset_type";


    public static void ModifyAssetXmlMainVersion(int mainVersion, string xmlPath)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(xmlPath);
        lock (doc)
        {
            XmlNode node = doc.SelectSingleNode("/" + rootTxt + "/" + mainVersionTxt);
            node.InnerText = mainVersion.ToString();
            doc.Save(xmlPath);
        }
    }

    public static bool FindAssetNode(string url, string xmlPath)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(xmlPath);
        lock (doc)
        {
            XmlNode assetsNode = doc.SelectSingleNode("/" + rootTxt + "/" + assetsTxt);
            foreach (XmlNode node in assetsNode.ChildNodes)
            {
                XmlElement cn = node as XmlElement;
                if (cn.Attributes[urlTxt].Value == url)
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static void ModifyAssetNode(Asset asset, string xmlPath)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(xmlPath);
        lock (doc)
        {
            XmlNode assetsNode = doc.SelectSingleNode("/" + rootTxt + "/" + assetsTxt);
            XmlNode selectNode = null;
            foreach (XmlNode node in assetsNode.ChildNodes)
            {
                XmlElement cn = node as XmlElement;
                if (cn.Attributes[urlTxt].Value == asset.assetUrl)
                {
                    selectNode = node;
                    break;
                }
            }
            if (selectNode != null)
            {
                foreach (var subNode in selectNode.ChildNodes)
                {
                    XmlElement xe = subNode as XmlElement;
                    switch (xe.Name)
                    {
                        case nameTxt:                                   // /Root/Assets/Asset/Name
                            xe.InnerText = asset.assetName;
                            break;
                        case md5Txt:                                    // /Root/Assets/Asset/MD5
                            xe.InnerText = asset.assetMD5;
                            break;
                        case versionTxt:                                // /Root/Assets/Asset/Version
                            xe.InnerText = asset.version.ToString();
                            break;
                        case sizeTxt:                                   // /Root/Assets/Asset/Size
                            xe.InnerText = asset.size.ToString();
                            break;
                        case forceTempTxt:                              // /Root/Assets/Asset/force_temp
                            xe.InnerText = asset.property.ToString();
                            break;
                        default:
                            break;
                    }
                }
                doc.Save(xmlPath);
            }
        }
    }

    public static void DeleteAssetNode(Asset asset, string xmlPath)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(xmlPath);
        lock (doc)
        {
            XmlNode assetsNode = doc.SelectSingleNode("/" + rootTxt + "/" + assetsTxt);     //	/Root/Assets
            XmlNode deleteNode = null;
            foreach (XmlNode node in assetsNode.ChildNodes)
            {
                XmlElement cn = node as XmlElement;                                             //	/Root/Assets/Asset
                if (cn.Attributes[urlTxt].Value == asset.assetUrl)
                {
                    deleteNode = node;
                    break;
                }
            }
            if (deleteNode != null)
            {
                assetsNode.RemoveChild(deleteNode);
                doc.Save(xmlPath);
            }
        }
    }

    public static void AddAssetNode(Asset asset, string xmlPath)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(xmlPath);
        lock (doc)
        {
            XmlNode assetsNode = doc.SelectSingleNode("/" + rootTxt + "/" + assetsTxt);

            XmlElement assetNode = doc.CreateElement(subAssetTxt);
            assetNode.SetAttribute(urlTxt, asset.assetUrl);

            XmlElement nameNode = doc.CreateElement(nameTxt);
            nameNode.InnerText = asset.assetName;
            assetNode.AppendChild(nameNode);

            XmlElement md5Node = doc.CreateElement(md5Txt);
            md5Node.InnerText = asset.assetMD5;
            assetNode.AppendChild(md5Node);

            XmlElement versionNode = doc.CreateElement(versionTxt);
            versionNode.InnerText = asset.version.ToString();
            assetNode.AppendChild(versionNode);

            XmlElement sizeNode = doc.CreateElement(sizeTxt);
            sizeNode.InnerText = asset.size.ToString();
            assetNode.AppendChild(sizeNode);

            XmlElement forceTempNode = doc.CreateElement(forceTempTxt);
            forceTempNode.InnerText = asset.property.ToString();
            assetNode.AppendChild(forceTempNode);

            assetsNode.AppendChild(assetNode);

            doc.Save(xmlPath);
        }
    }

    public static AssetXmlData LoadXml(string xmlPath)
    {
        AssetXmlData res = new AssetXmlData();
        XmlDocument doc = new XmlDocument();
        doc.Load(xmlPath);
        lock (doc)
        {
            XmlNode rootNode = doc.SelectSingleNode(rootTxt);
            foreach (var node in rootNode)
            {
                XmlElement xe = node as XmlElement;
                if (xe.Name == mainVersionTxt)
                {                               // /Root/MainVersion
                    res.mainversion = int.Parse(xe.InnerText);
                }
                else if (xe.Name == assetsTxt)
                {                               // /Root/Assets
                    foreach (var subNode in xe.ChildNodes)
                    {
                        XmlElement subXe = subNode as XmlElement;
                        if (subXe.Name == subAssetTxt)
                        {                       // /Root/Assets/Asset
                            Asset asset = new Asset();
                            asset.assetUrl = subXe.Attributes[urlTxt].Value;
                            foreach (var attr in subXe.ChildNodes)
                            {
                                XmlElement subsubXe = attr as XmlElement;
                                switch (subsubXe.Name)
                                {
                                    case nameTxt:                                   // /Root/Assets/Asset/Name
                                        asset.assetName = subsubXe.InnerText;
                                        break;
                                    case md5Txt:                                    // /Root/Assets/Asset/MD5
                                        asset.assetMD5 = subsubXe.InnerText;
                                        break;
                                    case versionTxt:                                // /Root/Assets/Asset/Version
                                        asset.version = int.Parse(subsubXe.InnerText);
                                        break;
                                    case sizeTxt:                                   // /Root/Assets/Asset/Size
                                        asset.size = ulong.Parse(subsubXe.InnerText);
                                        break;
                                    case forceTempTxt:                              // /Root/Assets/Asset/force_temp
                                        asset.property = (AssetProperty)Enum.Parse(typeof(AssetProperty), subsubXe.InnerText);
                                        break;
                                    default:
                                        break;
                                }
                            }
                            res.assetDatas.Add(asset);
                        }
                    }
                }
            }
        }

        return res;
    }

    public static void BuildXml(AssetXmlData data, string xmlPath)
    {
        int mainVersion = data.mainversion;
        List<Asset> assets = data.assetDatas;
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
        doc.AppendChild(dec);

        XmlElement root = doc.CreateElement(rootTxt);
        doc.AppendChild(root);

        XmlElement mv = doc.CreateElement(mainVersionTxt);
        mv.InnerText = mainVersion.ToString();
        root.AppendChild(mv);

        XmlElement assetsNode = doc.CreateElement(assetsTxt);
        foreach (var asset in assets)
        {
            XmlElement assetNode = doc.CreateElement(subAssetTxt);
            assetNode.SetAttribute(urlTxt, asset.assetUrl);

            XmlElement nameNode = doc.CreateElement(nameTxt);
            nameNode.InnerText = asset.assetName;
            assetNode.AppendChild(nameNode);

            XmlElement md5Node = doc.CreateElement(md5Txt);
            md5Node.InnerText = asset.assetMD5;
            assetNode.AppendChild(md5Node);

            XmlElement versionNode = doc.CreateElement(versionTxt);
            versionNode.InnerText = asset.version.ToString();
            assetNode.AppendChild(versionNode);

            XmlElement sizeNode = doc.CreateElement(sizeTxt);
            sizeNode.InnerText = asset.size.ToString();
            assetNode.AppendChild(sizeNode);

            XmlElement forceTempNode = doc.CreateElement(forceTempTxt);
            forceTempNode.InnerText = asset.property.ToString();
            assetNode.AppendChild(forceTempNode);

            assetsNode.AppendChild(assetNode);
        }
        root.AppendChild(assetsNode);
        string filePath = (xmlPath).Replace(@"\", @"/");
        string fileDir = filePath.Substring(0, filePath.LastIndexOf(@"/"));
        if (!Directory.Exists(fileDir))
        {
            Directory.CreateDirectory(fileDir);
        }
        doc.Save(xmlPath);
    }



}